package com.atguigu.lease.web.admin.controller;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 李壮
 * @CreateTime: 2025-05-12
 * @Version: 1.0
 */

@RestController
@RequestMapping("/admin/agreement")
@Tag(name = "租约管理")
public class LeaseAgreementController {

    @Autowired
    LeaseAgreementService leaseAgreementService;

    @Operation(summary = "根据id更新租约状态")
    @PostMapping("/updateStatusById")
    public Result updateStatusById(@RequestParam Long id, @RequestParam LeaseStatus leaseStatus){

        LambdaUpdateWrapper<LeaseAgreement> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(LeaseAgreement::getId,id);
        lambdaUpdateWrapper.set(LeaseAgreement::getStatus,leaseStatus);
        leaseAgreementService.update(lambdaUpdateWrapper);
        return Result.ok();
    }

    @Operation(summary = "保存或更新租约信息")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody LeaseAgreement leaseAgreement){
        leaseAgreementService.saveOrUpdate(leaseAgreement);
        return Result.ok();
    }


    @Operation(summary = "根据id查询租约信息")
    @GetMapping
    public Result getAgreementById(@RequestParam Long id){
        AgreementVo agreementVo = leaseAgreementService.getAgreementById(id);
        return Result.ok(agreementVo);
    }


    @Operation(summary = "根据条件分页查询租约列表")
    @GetMapping("/page")
    public Result page(@RequestParam Long current,@RequestParam Long size,AgreementQueryVo agreementQueryVo){

        IPage<AgreementVo> page = new Page<>(current,size);
        IPage<AgreementVo> list = leaseAgreementService.pageLeaseAgreementByQuery(page,agreementQueryVo);
        return Result.ok(list);
    }

    @Operation(summary = "根据ID删除租约信息")
    @DeleteMapping("/removeById")
    public Result removeById(@RequestParam Long id){
        leaseAgreementService.removeById(id);
        return Result.ok();
    }


}
